package com.lenovo.interceptor.tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Descpription
 * @Author CJF
 * @Date 2019/2/27 14:11
 **/
@Service
public class CheckToken {

    @Autowired
    RedisTemplate redisTemplate;

    public boolean checkLoginStatus(HttpServletRequest request) {
        try {
            Cookie[] cookie = request.getCookies();
            String key = "Lenovo-sessionId:" + GetCookieVallue.getValueByName(cookie, "Lenovo-sessionId").toString();
            if (redisTemplate.opsForValue().get(key) == null) {
                key = "Lenovo-sessionId:" + request.getSession().getId();
                return redisTemplate.opsForValue().get(key) != null;
            } else {
                //System.out.println(key);
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
